package com.audiobo.recorder;

import java.io.*;
import javax.sound.sampled.*;

// Referenced classes of package com.audiobo.recorder:
//            AppletController, AudioPlaybackDelegate

class AudioPlayback implements Runnable {

  AudioPlaybackDelegate _flddelegate;
  SourceDataLine line;
  Thread thread;
  AudioInputStream audioInputStream;
  File inputFile;
  AudioFormat audioFormat;
  int bufferLengthInBytes;
  double currentTime;
  double duration;

  AudioPlayback() {
  }

  public boolean start() {
    inputFile = new File(AppletController.audioFilePath);
    if (inputFile.exists()) {
      // doing nothing
    } else {
      return false;
    }

    try {
      audioInputStream = AudioSystem.getAudioInputStream(inputFile);
    } catch (Exception exception1) {
      System.err.println(exception1.toString());
      return false;
    }

    audioFormat = audioInputStream.getFormat();
    javax.sound.sampled.DataLine.Info info = new javax.sound.sampled.DataLine.Info(
        javax.sound.sampled.SourceDataLine.class, audioFormat);
    if (!AudioSystem.isLineSupported(info)) {
      System.err.println((new StringBuilder()).append("Line matching ")
          .append(info).append(" not supported.").toString());
      return false;
    }
    try {
      line = (SourceDataLine) AudioSystem.getLine(info);
      line.open(audioFormat);
    } catch (LineUnavailableException lineunavailableexception) {
      System.err.println((new StringBuilder())
          .append("Unable to open the line: ").append(lineunavailableexception)
          .toString());
      return false;
    } catch (Exception exception2) {
      System.err.println(exception2.toString());
      return false;
    }
    audioFormat = line.getFormat();
    int i = audioFormat.getFrameSize();
    bufferLengthInBytes = line.getBufferSize();
    line.start();
    (new Thread(this, "AudioPlayback Thread")).start();
    return true;
  }

  public void stop() {
    if (audioInputStream != null) {
      try {
        audioInputStream.close();
      } catch (IOException ioexception) {
        System.out.println("IOException closing audioInputStream");
      }
      audioInputStream = null;
    }
  }

  public void run() {
    byte abyte0[];
    abyte0 = new byte[bufferLengthInBytes];
    boolean flag = false;
    currentTime = 0.0D;

    try {
      while (true) {
        if (audioInputStream == null || !line.isOpen()) {
          break; /* Loop/switch isn't completed */
        }
        int i;
        i = audioInputStream.read(abyte0, 0, bufferLengthInBytes);
        if (i == -1) {
          System.out.println("[Thread] error reading input stream");
          break; /* Loop/switch isn't completed */
        }
        for (int j = i; j > 0; j -= line.write(abyte0, 0, j)) {
        }
        currentTime += (double) i
            / (double) ((float) audioFormat.getFrameSize() * audioFormat
                .getFrameRate());
        if (_flddelegate != null) {
          _flddelegate.updatePlaybackPosition(currentTime, duration);
        }
      }
    } catch (Exception e) {
      System.err.println(e.toString());
    }

    if (audioInputStream != null) {
      try {
        audioInputStream.close();
      } catch (IOException ioexception) {
        System.out.println("IOException closing audioInputStream");
      }
      audioInputStream = null;
    }
    if (line != null) {
      line.drain();
      line.stop();
      line.close();
    }
    _flddelegate.playbackFinished();
    return;
  }

  public void setDelegate(AudioPlaybackDelegate audioplaybackdelegate) {
    _flddelegate = audioplaybackdelegate;
  }

  public void setDuration(double d) {
    duration = d;
  }
}
